let make_vector (x0, y0) (x1, y1) =
  (x1 -. x0, y1 -. y0)

let vector_length (x, y) =
  sqrt (x *. x +. y *. y)

let offset_point (x, y) (px, py) =
  (px +. x, py +. y)

let scale_to_length l (a, b) =
  let currentlength = vector_length (a, b) in
    if currentlength = 0. then (a, b) else
      let factor = l /. currentlength in
        (a *. factor, b *. factor)

(* q1 *)
let round_to_nearest n = 
  if n >= floor n +. 0.5 then ceil n
  else floor n

(* q2 *)
let q2 (x1, y1) (x2, y2) = 
  let x, y =  (x2 +. x1, y2 +. y1) in
  x /. 2., y /. 2.

(* q3 *)
let separate f = 
  let f' = floor f in
  (f', f -. f')

(* q4 *)
let star x =
  let i = int_of_float (floor (x *. 50.)) in
    let i' = if i = 50 then 49 else i in
      for x = 1 to i' - 1 do print_char ' ' done;
      print_char '*';
      print_newline ()

(* q5 *)
let plot f a b dy =
  let pos = ref a in
    while !pos < b do
      star (f !pos);
      pos := !pos +. dy 
    done
